package com.yinhe.music.yhmusic.downmanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusListObject;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseFragment;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.db.bean.DownloadMusicEntity;
import com.yinhe.music.yhmusic.dialog.more.MoreDialog;
import com.yinhe.music.yhmusic.downmusic.DownloadStatus;
import com.yinhe.music.yhmusic.downmusic.DownloadTask;
import com.yinhe.music.yhmusic.log.BlankUtil;
import com.yinhe.music.yhmusic.manager.ManageActivity;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.utils.DataHolder;
import com.yinhe.music.yhmusic.utils.MusicUtils;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/2/19.
 */

public class DownloadFinishFragment extends BaseFragment {
    private RecyclerView mRecyclerView;
    private DownListAdapter mAdapter;
    private LinearLayout mManager, mPlay;
    private List<DownloadMusicEntity> mDownMusic = new ArrayList<>();
    private List<Music> musicList = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.new_music_item, container, false);
        RxBus.get().register(this);
        mPlay = view.findViewById(R.id.play_layout);
        mManager = view.findViewById(R.id.manager);
        mRecyclerView = view.findViewById(R.id.recyclerView);
        initData();
        setRecyclerView();
        setListener();
        return view;
    }

    @Override
    public void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    private void setRecyclerView() {
        WrapContentLinearLayoutManager linearLayoutManager = new WrapContentLinearLayoutManager(getActivity());
        mAdapter = new DownListAdapter(mDownMusic);
        mAdapter.setEmptyView(getEmptyView(mRecyclerView));
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST, 0, true));
    }


    private void setListener() {
        mManager.setOnClickListener((v) -> {
                Intent intent = new Intent(getActivity(), ManageActivity.class);
                List<Music> mList = new ArrayList<>();
                for (DownloadMusicEntity downloadMusicEntity : mDownMusic) {
                    mList.add(new Music(downloadMusicEntity));
                }
                DataHolder.getInstance().setData(mList);
                intent.putExtra("manager_type", IConstants.DOWN_FINISH_MANAGER);
                startActivity(intent);
        });

        mAdapter.setOnItemClickListener((adapter, v, position) -> {
            if (mService != null) {
                mService.playPosition(musicList, position);
            }
        });

        mAdapter.setOnItemChildClickListener((adapter, v, position) -> {
            DownloadMusicEntity downloadMusicEntity = (DownloadMusicEntity) adapter.getData().get(position);
            Music music = new Music(downloadMusicEntity);
            music.setFileType(Music.Type.LOCAL);
            MoreDialog.newInstance(music, 0, IConstants.DOWN_FINISH_MANAGER).show(getFragmentManager(), "MoreDialog");
        });

        mPlay.setOnClickListener((v) -> {
            if (mService != null) {
                mService.playAll(musicList);
            }
        });
    }

    private void initData() {
        List<DownloadMusicEntity> info = DataBaseAccessor.getInstance().getDownMusicByDownloadStatus(DownloadStatus.DOWNLOAD_STATUS_COMPLETED);
        if (info != null) {
            mDownMusic = info;
            musicList = MusicUtils.getMusicListFromDownDb(mDownMusic);
        }
        RxBus.get().post(RxBusEventType.Music.GET_DOWN_FINISH, RxbusNullObject.INSTANCE);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {@Tag(RxBusEventType.Download.DOWNLOAD_COMPLETE)})
    public void onRxbusDownloadComplete(DownloadTask downloadTask) {
        DownloadMusicEntity musicEntity = DataBaseAccessor.getInstance().getDownMusicByDownloadIdSyn(downloadTask.getId());
        if (musicEntity != null) {
            mAdapter.addData(musicEntity);
            musicList.add(new Music(musicEntity));
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {@Tag(RxBusEventType.Music.DELETE_DOWN_MUSIC)})
    public void onRxbusDeleteDownMusic(RxbusListObject rxbusListObject) {
        if (rxbusListObject != null) {
            if (BlankUtil.isBlank(mDownMusic)) {
                return;
            }
            for (DownloadMusicEntity i : mDownMusic) {
                for (Object j : rxbusListObject.rxList) {
                    Integer songId = (Integer) j;
                    if (i.getSongid() == songId) {
                        mDownMusic.remove(i);
                        musicList.remove(i);
                    }
                }
            }
            mAdapter.notifyDataSetChanged();
        }
    }

}
